package com.yequan.file.large;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author yq
 * @date 2020/09/25
 */
public class ReadLargeTextWithNio {

    public static void main(String[] args) throws IOException {
        FileInputStream fin = new FileInputStream("E:\\TEMP\\read.txt");
        FileChannel fcin = fin.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024*1024);
        while (true) {
            buffer.clear();
            int flag = fcin.read(buffer);
            if (flag == -1) {
                break;
            }
            buffer.flip();
            FileOutputStream fout = new FileOutputStream("E:\\TEMP\\read" + Math.abs(Math.random()) + ".txt");
            FileChannel fcout = fout.getChannel();
            fcout.write(buffer);
        }
    }

}
